<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="180px">
    <!-- 必填字段 -->
    <el-form-item label="手机号码" prop="mobilePhone">
      <el-input type="text" v-model="dataForm.mobilePhone" placeholder="用户手机号码，不能重复" maxlength="11" show-word-limit></el-input>
    </el-form-item>
    <el-form-item label="来源文件" prop="sourceFile">
      <el-input v-model="dataForm.sourceFile" placeholder="来源文件" maxlength="60" show-word-limit></el-input>
    </el-form-item>
    <el-form-item label="姓名" prop="name">
      <el-input v-model="dataForm.name" placeholder="姓名" maxlength="16" show-word-limit></el-input>
    </el-form-item>
    <el-form-item label="区域" prop="area">
      <el-input v-model="dataForm.area" placeholder="区域" maxlength="16" show-word-limit></el-input>
    </el-form-item>
    <el-form-item label="城市" prop="city">
      <el-input v-model="dataForm.city" placeholder="城市" maxlength="16" show-word-limit></el-input>
    </el-form-item>
    <el-form-item label="获客渠道" prop="channel">
      <el-input v-model="dataForm.channel" placeholder="获客渠道" maxlength="16" show-word-limit></el-input>
    </el-form-item>
    <el-form-item label="名单来源" prop="channelSource">
      <el-input v-model="dataForm.channelSource" placeholder="名单来源" maxlength="16" show-word-limit></el-input>
    </el-form-item>
    <el-form-item label="获客标签" prop="label">
      <el-input v-model="dataForm.label" placeholder="获客标签" maxlength="32" show-word-limit></el-input>
    </el-form-item>

    <!-- 选填字段 -->
    <el-form-item label="运营商" prop="operatorName">
      <el-select v-model="dataForm.operatorName" placeholder="请选择">
        <el-option
          v-for="item in operatorNames"
          :key="item.key"
          :label="item.value"
          :value="item.key">
        </el-option>
      </el-select>
    </el-form-item>
    <el-form-item label="运营商品牌" prop="brandName">
      <el-input v-model="dataForm.brandName" placeholder="运营商品牌名称" maxlength="16" show-word-limit></el-input>
    </el-form-item>
    <el-form-item label="运营商其他信息" prop="operatorInfo">
      <el-input type="textarea" v-model="dataForm.operatorInfo" placeholder="运营商其他信息" maxlength="128" show-word-limit></el-input>
    </el-form-item>
    <el-form-item label="生日" prop="birthDay">
      <el-date-picker
        v-model="dataForm.birthDay"
        type="date"
        placeholder="选择出生日期"
        format="yyyy-MM-dd"
        value-format="timestamp">
      </el-date-picker>
    </el-form-item>
    <el-form-item label="性别" prop="gender">
      <el-select v-model="dataForm.gender" placeholder="请选择">
        <el-option
          v-for="item in genders"
          :key="item.key"
          :label="item.value"
          :value="item.key">
        </el-option>
      </el-select>
    </el-form-item>
    <el-form-item label="身份证号码" prop="idNumber">
      <el-input type="text" v-model="dataForm.idNumber" placeholder="身份证号码" maxlength="18" show-word-limit></el-input>
    </el-form-item>
    <el-form-item label="是否购房" prop="ownHouse">
      <el-select v-model="dataForm.ownHouse" placeholder="请选择">
        <el-option
          v-for="item in commonBool"
          :key="item.key"
          :label="item.value"
          :value="item.key">
        </el-option>
      </el-select>
    </el-form-item>
    <el-form-item label="房产购买时间" prop="houseBuyTime">
      <el-date-picker
        v-model="dataForm.houseBuyTime"
        type="date"
        placeholder="选择房产购买日期"
        format="yyyy-MM-dd"
        value-format="timestamp">
      </el-date-picker>
    </el-form-item>
    <el-form-item label="税后收入" prop="monthlyIncome">
      <el-input-number v-model="dataForm.monthlyIncome" :min="0" :max="1000000" placeholder="月税后收入" :controls="false"></el-input-number>
    </el-form-item>
    <el-form-item label="是否购车" prop="ownCar">
      <el-select v-model="dataForm.ownCar" placeholder="请选择">
        <el-option
          v-for="item in commonBool"
          :key="item.key"
          :label="item.value"
          :value="item.key">
        </el-option>
      </el-select>
    </el-form-item>
    <el-form-item label="车辆是否抵押" prop="carPledge">
      <el-select v-model="dataForm.carPledge" placeholder="请选择">
        <el-option
          v-for="item in commonBool"
          :key="item.key"
          :label="item.value"
          :value="item.key">
        </el-option>
      </el-select>
    </el-form-item>
    <el-form-item label="公积金最近连续缴纳月数" prop="depositPeriod">
      <el-input-number v-model="dataForm.depositPeriod" :min="0" :max="360" placeholder="公积金最近连续缴纳月数"></el-input-number>
    </el-form-item>
    <el-form-item label="平安寿险购买时间" prop="pinganInsuranceTime">
      <el-date-picker
        v-model="dataForm.pinganInsuranceTime"
        type="date"
        placeholder="选择寿险购买日期"
        format="yyyy-MM-dd"
        value-format="timestamp">
      </el-date-picker>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        commonBool: window.ENUMS['COMMON_BOOL'],
        genders: window.ENUMS['GENDER'],
        operatorNames: window.ENUMS['TEL_OP_TYPE'],
        dataForm: {
          id: 0,
          mobilePhone: '',
          sourceFile: '',
          name: '',
          area: '',
          city: '',
          operatorName: '',
          brandName: '',
          operatorInfo: '',
          birthDay: '',
          gender: '',
          idNumber: '',
          channel: '',
          channelSource: '',
          ownHouse: '',
          houseBuyTime: '',
          monthlyIncome: '',
          ownCar: '',
          carPledge: '',
          depositPeriod: '',
          pinganInsuranceTime: '',
          label: '',
          createUserId: '',
          createTime: ''
        },
        dataRule: {
          mobilePhone: [
            { required: true, message: '用户手机号码，唯一且不能为空', trigger: 'blur' }
          ],
          sourceFile: [
              { required: true, message: '用户来源文件不能为空', trigger: 'blur' }
          ],
          name: [
            { required: true, message: '姓名不能为空', trigger: 'blur' }
          ],
          area: [
            { required: true, message: '客户所属区域不能为空', trigger: 'blur' }
          ],
          city: [
            { required: true, message: '客户所属城市不能为空', trigger: 'blur' }
          ],
          channel: [
            { required: true, message: '获客渠道不能为空', trigger: 'blur' }
          ],
          channelSource: [
            { required: true, message: '获客来源不能为空', trigger: 'blur' }
          ],
          label: [
            { required: true, message: '客户标签不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/customer/customer/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.mobilePhone = data.customer.mobilePhone
                this.dataForm.sourceFile = data.customer.sourceFile
                this.dataForm.name = data.customer.name
                this.dataForm.area = data.customer.area
                this.dataForm.city = data.customer.city
                this.dataForm.operatorName = data.customer.operatorName
                this.dataForm.brandName = data.customer.brandName
                this.dataForm.operatorInfo = data.customer.operatorInfo
                this.dataForm.birthDay = data.customer.birthDay
                this.dataForm.gender = data.customer.gender
                this.dataForm.idNumber = data.customer.idNumber
                this.dataForm.channel = data.customer.channel
                this.dataForm.channelSource = data.customer.channelSource
                this.dataForm.ownHouse = data.customer.ownHouse
                this.dataForm.houseBuyTime = data.customer.houseBuyTime
                this.dataForm.monthlyIncome = data.customer.monthlyIncome
                this.dataForm.ownCar = data.customer.ownCar
                this.dataForm.carPledge = data.customer.carPledge
                this.dataForm.depositPeriod = data.customer.depositPeriod
                this.dataForm.pinganInsuranceTime = data.customer.pinganInsuranceTime
                this.dataForm.label = data.customer.label
                this.dataForm.createUserId = data.customer.createUserId
                this.dataForm.createTime = data.customer.createTime
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/customer/customer/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'mobilePhone': this.dataForm.mobilePhone,
                'sourceFile': this.dataForm.sourceFile,
                'name': this.dataForm.name,
                'area': this.dataForm.area,
                'city': this.dataForm.city,
                'operatorName': this.dataForm.operatorName,
                'brandName': this.dataForm.brandName,
                'operatorInfo': this.dataForm.operatorInfo,
                'birthDay': this.dataForm.birthDay,
                'gender': this.dataForm.gender,
                'idNumber': this.dataForm.idNumber,
                'channel': this.dataForm.channel,
                'channelSource': this.dataForm.channelSource,
                'ownHouse': this.dataForm.ownHouse,
                'houseBuyTime': this.dataForm.houseBuyTime,
                'monthlyIncome': this.dataForm.monthlyIncome,
                'ownCar': this.dataForm.ownCar,
                'carPledge': this.dataForm.carPledge,
                'depositPeriod': this.dataForm.depositPeriod,
                'pinganInsuranceTime': this.dataForm.pinganInsuranceTime,
                'label': this.dataForm.label,
                'createUserId': this.dataForm.createUserId,
                'createTime': this.dataForm.createTime
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
